/**
 * 
 */
package com.cp16.tss.validation;

import java.awt.Color;
import java.util.Date;

import com.cp16.tss.common.Constants;
import com.cp16.tss.form.RequirementAddNewForm;

/**
 * @author ADMIN
 * 
 */
public class RequirementValidation {

	public static boolean validateCustomerId(RequirementAddNewForm requirementAddNewForm) {
		boolean customerIdFlag = true;
		String customerId = requirementAddNewForm.getTxtCustomerId().getText();
		if (customerId.equals(Constants.BLANK)) {
			requirementAddNewForm.getTxtCustomerId().setBackground(Color.PINK);
			customerIdFlag = false;
		} else {
			if (!CommonValidation.isNummeric(customerId)) {
				customerIdFlag = false;
			}
		}
		return customerIdFlag;
	}

	public static boolean validateDateRequirement(RequirementAddNewForm requirementAddNewForm) {
		boolean dateRequirementFlag = true;
		Date dateRequirement = requirementAddNewForm.getTxtDateRequirement().getDate();
		if (dateRequirement == null) {
			requirementAddNewForm.getTxtDateRequirement().setBackground(Color.PINK);
			dateRequirementFlag = false;
		}
		return dateRequirementFlag;
	}

	public static boolean validateDeadLine(RequirementAddNewForm requirementAddNewForm) {
		boolean deadLineFlag = true;
		Date deadline = requirementAddNewForm.getTxtDeadLine().getDate();
		if (deadline == null) {
			requirementAddNewForm.getTxtDeadLine().setBackground(Color.PINK);
			deadLineFlag = false;
		}
		return deadLineFlag;
	}

	public static boolean validateDateReqAndDeadline(RequirementAddNewForm requirementAddNewForm) {
		boolean dateFlag;
		Date dateReq = requirementAddNewForm.getTxtDateRequirement().getDate();
		Date deadline = requirementAddNewForm.getTxtDeadLine().getDate();
		if (dateReq.before(deadline) || dateReq.equals(deadline)) {
			dateFlag = true;
		} else {
			dateFlag = false;
			requirementAddNewForm.getLblAddNewRequirement().setText("value of date not right");
		}
		return dateFlag;
	}

	public static boolean validateAmount(RequirementAddNewForm requirementAddNewForm) {
		boolean amountFlag = true;
		if(requirementAddNewForm.getChckbxDeviceList().isSelected()){
			String amount1 = requirementAddNewForm.getTxtAmount1().getText();
			String amount2 = requirementAddNewForm.getTxtAmount2().getText();
			String amount3 = requirementAddNewForm.getTxtAmount3().getText();
			String amount4 = requirementAddNewForm.getTxtAmount4().getText();
			String amount5 = requirementAddNewForm.getTxtAmount5().getText();
			String deviceName1 = requirementAddNewForm.getTxtAmount1().getText();
			String deviceName2 = requirementAddNewForm.getTxtAmount2().getText();
			String deviceName3 = requirementAddNewForm.getTxtAmount3().getText();
			String deviceName4 = requirementAddNewForm.getTxtAmount4().getText();
			String deviceName5 = requirementAddNewForm.getTxtAmount5().getText();
			boolean amount1Flag = deviceName1.equals(Constants.BLANK) || (!deviceName1.equals(Constants.BLANK) && CommonValidation.isNummeric(amount1));
			boolean amount2Flag = deviceName2.equals(Constants.BLANK) || (!deviceName2.equals(Constants.BLANK) && CommonValidation.isNummeric(amount2));
			boolean amount3Flag = deviceName3.equals(Constants.BLANK) || (!deviceName3.equals(Constants.BLANK) && CommonValidation.isNummeric(amount3));
			boolean amount4Flag = deviceName4.equals(Constants.BLANK) || (!deviceName4.equals(Constants.BLANK) && CommonValidation.isNummeric(amount4));
			boolean amount5Flag = deviceName5.equals(Constants.BLANK) || (!deviceName5.equals(Constants.BLANK) && CommonValidation.isNummeric(amount5));
			amountFlag = amount1Flag && amount2Flag && amount3Flag && amount4Flag && amount5Flag;

			if (!amount1Flag) {
				requirementAddNewForm.getTxtAmount1().setBackground(Color.PINK);
			}
			if (!amount2Flag) {
				requirementAddNewForm.getTxtAmount2().setBackground(Color.PINK);
			}
			if (!amount3Flag) {
				requirementAddNewForm.getTxtAmount3().setBackground(Color.PINK);
			}
			if (!amount4Flag) {
				requirementAddNewForm.getTxtAmount4().setBackground(Color.PINK);
			}
			if (!amount5Flag) {
				requirementAddNewForm.getTxtAmount5().setBackground(Color.PINK);
			}
		}
		
		return amountFlag;
	}

	public static boolean validatePrice(RequirementAddNewForm requirementAddNewForm) {
		boolean priceFlag = true;
		if(requirementAddNewForm.getChckbxDeviceList().isSelected()){
			String deviceName1 = requirementAddNewForm.getTxtAmount1().getText();
			String deviceName2 = requirementAddNewForm.getTxtAmount2().getText();
			String deviceName3 = requirementAddNewForm.getTxtAmount3().getText();
			String deviceName4 = requirementAddNewForm.getTxtAmount4().getText();
			String deviceName5 = requirementAddNewForm.getTxtAmount5().getText();
			String price1 = requirementAddNewForm.getTxtPrice1().getText();
			String price2 = requirementAddNewForm.getTxtPrice2().getText();
			String price3 = requirementAddNewForm.getTxtPrice3().getText();
			String price4 = requirementAddNewForm.getTxtPrice4().getText();
			String price5 = requirementAddNewForm.getTxtPrice5().getText();

			boolean price1Flag = deviceName1.equals(Constants.BLANK) || (!deviceName1.equals(Constants.BLANK)
					&& CommonValidation.isNummeric(price1));
			boolean price2Flag = deviceName2.equals(Constants.BLANK) || (!deviceName2.equals(Constants.BLANK)
					&& CommonValidation.isNummeric(price2));
			boolean price3Flag = deviceName3.equals(Constants.BLANK) || (!deviceName3.equals(Constants.BLANK)
					&& CommonValidation.isNummeric(price3));
			boolean price4Flag = deviceName4.equals(Constants.BLANK) || (!deviceName4.equals(Constants.BLANK)
					&& CommonValidation.isNummeric(price4));
			boolean price5Flag = deviceName5.equals(Constants.BLANK) || (!deviceName5.equals(Constants.BLANK)
					&& CommonValidation.isNummeric(price5));
			priceFlag = price1Flag && price2Flag && price3Flag && price4Flag && price5Flag;

			if (!price1Flag) {
				requirementAddNewForm.getTxtPrice1().setBackground(Color.PINK);
			}
			if (!price2Flag) {
				requirementAddNewForm.getTxtPrice2().setBackground(Color.PINK);
			}
			if (!price3Flag) {
				requirementAddNewForm.getTxtPrice3().setBackground(Color.PINK);
			}
			if (!price4Flag) {
				requirementAddNewForm.getTxtPrice4().setBackground(Color.PINK);
			}
			if (!price5Flag) {
				requirementAddNewForm.getTxtPrice5().setBackground(Color.PINK);
			}
		}
		
		return priceFlag;
	}

	public static boolean validatePhone(RequirementAddNewForm requirementAddNewForm) {
		boolean phoneFlag = true;
		String phoneText = requirementAddNewForm.getTxtPhoneList().getText();
		if (!phoneText.equals(Constants.BLANK)) {
			if(phoneText.length() > 5){
				String charReplace = phoneText.substring(5, 6);
				phoneText = phoneText.replace(charReplace, Constants.BLANK);
			}
			int loopNumber = phoneText.toString().length() / 5;
			int du = phoneText.toString().length() % 5;
			if (loopNumber <= 0 || du != 0 || !CommonValidation.isNummeric(phoneText)) {
				phoneFlag = false;
			}
		}

		return phoneFlag;
	}

	public static boolean validateRequirementAddNewForm(RequirementAddNewForm requirementAddNewForm) {
		boolean customerIdFlag = validateCustomerId(requirementAddNewForm);
		boolean dateRequirementFlag = validateDateRequirement(requirementAddNewForm);
		boolean deadLineFlag = validateDeadLine(requirementAddNewForm);
		boolean amountFlag = validateAmount(requirementAddNewForm);
		boolean priceFlag = validatePrice(requirementAddNewForm);
		boolean phoneFlag = validatePhone(requirementAddNewForm);
		boolean requirementAddNewFormFlag = customerIdFlag && dateRequirementFlag && deadLineFlag && amountFlag && priceFlag && phoneFlag;
		return requirementAddNewFormFlag;
	}
}
